package com.mpp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mpp.common.exception.YyghException;
import com.mpp.common.helper.JwtHelper;
import com.mpp.common.result.ResultCodeEnum;
import com.mpp.common.util.Md5Utils;
import com.mpp.common.util.StringUtils;
import com.mpp.model.entity.user.UserInfo;
import com.mpp.model.enums.AuthStatusEnum;
import com.mpp.model.vo.user.LoginVo;
import com.mpp.model.vo.user.UserAuthVo;
import com.mpp.user.mapper.UserInfoMapper;
import com.mpp.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author MMDZ
 * @date 2022/8/16 10:14
 * @description TODO
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Map<String, Object> loginUser(LoginVo loginVo) {
        //从loginVO中获取手机号和验证码
        String phone = loginVo.getPhone();
        String code = loginVo.getCode();

        //判断手机号和验证码和密码是否为空
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        //判断验证码是否正确
        String redisCode = redisTemplate.opsForValue().get(phone);
        if(StringUtils.isEmpty(redisCode)){
            throw new YyghException(ResultCodeEnum.FAILMSM);
        }

        //判断数据库是否有数据：根据手机号查询数据库，如果不存在相同手机号就是没有，则需要先注册
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        UserInfo userInfo = baseMapper.selectOne(wrapper);
        if (userInfo == null){//第一次使用这个手机号登录
            userInfo = new UserInfo();
            userInfo.setName("");
           userInfo.setPhone(phone);
           userInfo.setStatus(1);
            baseMapper.insert(userInfo);
        }

        //密码错误
       /* if(!Md5Utils.encrypt(loginVo.getCode()).equals(Md5Utils.encrypt(userInfo.getPwd()))){
            throw new YyghException(ResultCodeEnum.LOGIN_ERROR);
        }*/

        //判断是否是被禁用的用户
        if(userInfo.getStatus() == 0){
            throw new YyghException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }

        //不是第一次，直接登录
        //返回登录信息
        //返回登录用户名
        //返回token信息
        Map<String,Object> map = new HashMap<>();
        String name = userInfo.getName();
        if (StringUtils.isEmpty(name)){
            name = userInfo.getNickName();
        }
        if(StringUtils.isEmpty(name)){
            name = userInfo.getPhone();
        }
        map.put("name",name);
        //token生成
        map.put("token", JwtHelper.createToekn(userInfo.getId(),userInfo.getName()));
        return map;
    }

    @Override
    public Boolean registerUser(LoginVo loginVo) {
        String phone = loginVo.getPhone();
        String code = loginVo.getCode();

        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPwd(Md5Utils.encrypt(code));
        int state = baseMapper.insert(userInfo);
        if (state > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean userAuth(Long userId, UserAuthVo userAuthVo) {
        UserInfo userInfo = baseMapper.selectById(userId);
        userInfo.setName(userAuthVo.getName());
        userInfo.setCertificatesType(userAuthVo.getCertificatesType());
        userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
        userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
        userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());
        int i = baseMapper.updateById(userInfo);
        if (i > 0) {
            return true;
        }
        return false;
    }

}
